﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Globalization;

namespace SportingGoodsExhibitionSystem
{
    /// <summary>
    /// FunctionTwo.xaml 的交互逻辑
    /// </summary>
    public partial class FunctionTwo : Page
    {
        public FunctionTwo()
        {
            InitializeComponent();
        }
        string strCon = "server=.;database=CDB;user=sa;pwd=123456";
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            
            SqlConnection con = new SqlConnection(strCon);
            con.Open();
            if(status==0)
            {
                SqlCommand cmd = new SqlCommand("insert into vacation(studentId,studentNa,studentCollege,reason,time,status,timeend) values(@id,@name,@coll,@rea,@ti,@sta,@end)", con);
                cmd.Parameters.Add("@id", SqlDbType.Int, 10).Value = stuNum;
                cmd.Parameters.Add("@name", SqlDbType.VarChar, 10).Value = stuName;
                cmd.Parameters.Add("@coll", SqlDbType.VarChar, 10).Value = stuCollege;
                cmd.Parameters.Add("@rea", SqlDbType.VarChar, 50).Value = textReason.Text;
                cmd.Parameters.Add("@ti", SqlDbType.VarChar, 50).Value = textTime.Text;
                cmd.Parameters.Add("@sta", SqlDbType.Int, 10).Value = 2;
                cmd.Parameters.Add("@end", SqlDbType.VarChar, 50).Value = textTimeend.Text;
                cmd.ExecuteNonQuery();
                SqlCommand cmd1 = new SqlCommand("update [studentInfo] set leaveStatus=@num where studentNumber=@id1", con);
                cmd1.Parameters.Add("@id1", SqlDbType.Int, 10).Value = stuNum;
                cmd1.Parameters.Add("@num", SqlDbType.Int, 10).Value = 2;
                cmd1.ExecuteNonQuery();
                MessageBox.Show("已申请，待审核");
            }
            else if(status==2)
            {
                MessageBox.Show("正在申请中，无法多次申请");
            }else if(status==1)
            {
                MessageBox.Show("已请假，无法多次申请");
            }
           


            //ExecuteNonQuery指令执行,更新操作

            con.Close();

            textReason.Text  = "";
            textTime.Text = "";
            textTimeend.Text = "";

        }
        string stuNum,stuName,stuCollege;
        int num ;

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {

                num = Convert.ToInt32(stuNum);
                SqlConnection con = new SqlConnection(strCon);
                con.Open();
                SqlCommand cmd = new SqlCommand("select leaveStatus from [studentInfo]  where studentNumber=@id1", con);
                cmd.Parameters.Add("@id1", SqlDbType.Int, 10).Value = num;
                SqlDataAdapter read = new SqlDataAdapter(cmd);
                DataSet dataset = new DataSet();
                read.Fill(dataset);
                DataTable table = dataset.Tables[0];
                DataRowCollection rows = table.Rows;
                DataRow row = rows[0];
                int tem = Convert.ToInt32(row["leaveStatus"]);
                if (tem == 2)
                {
                    SqlCommand cmd1 = new SqlCommand("delete  [vacation]  where studentNumber=@id1 and status=@sta", con);
                    cmd1 = new SqlCommand("delete vacation where studentId=@id and status=@sta", con);
                    cmd1.Parameters.Add("@id", SqlDbType.VarChar, 50).Value = num;
                    cmd1.Parameters.Add("@sta", SqlDbType.VarChar, 50).Value = 2;
                    cmd1.ExecuteNonQuery();
                    SqlCommand cmd2 = new SqlCommand("update [studentInfo] set leaveStatus=@num where studentNumber=@id1", con);
                    cmd2.Parameters.Add("@id1", SqlDbType.Int, 10).Value = num;
                    cmd2.Parameters.Add("@num", SqlDbType.Int, 10).Value = 0;
                    cmd2.ExecuteNonQuery();
                    MessageBox.Show("取消成功");
                }
                else if (tem == 1)
                {
                    MessageBox.Show("正在请假中，请选择销假");
                }
                else if (tem == 0)
                {
                    MessageBox.Show("暂未申请");
                }
                con.Close();
            
        }

        int status;
        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            int sta = 1;
            SqlConnection con = new SqlConnection(strCon);
            con.Open();
            SqlCommand cmd = new SqlCommand("select * from studentInfo where studentStatus=@num", con);
            cmd.Parameters.Add("@num", SqlDbType.Int, 10).Value = sta;
            SqlDataAdapter read = new SqlDataAdapter(cmd);
            //DataTable dt = new DataTable();
            DataSet dataset = new DataSet();
            read.Fill(dataset);
            DataTable table = dataset.Tables[0];
            DataRowCollection rows = table.Rows;
            DataRow row = rows[0];
            stuNum = row["studentNumber"].ToString();
            labelNum.Content = stuNum;
            stuName = row["studentName"].ToString();
            labelCollege.Content = row["studyCollege"].ToString();
            stuCollege = row["studyCollege"].ToString();
            labelName.Content = row["studentName"].ToString();
            status = Convert.ToInt32(row["leaveStatus"]);
            
            if (row["leaveStatus"].ToString() == "2")
            {
                btnCancel.Visibility = Visibility.Visible;
            }
            else
            {
                btnCancel.Visibility = Visibility.Hidden;
            }
            con.Close();

        }
    }
    public class timeConver : IValueConverter
    {
        object IValueConverter.Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null)
                return DependencyProperty.UnsetValue;
            DateTime date = (DateTime)value;
            return date.ToString("yyyy年MM月dd日");
        }

        object IValueConverter.ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
